package com.zzhua.cglib;

import com.zzhua.cglib.callback.Callback_0;
import com.zzhua.cglib.callback.Callback_2;
import com.zzhua.cglib.service.OrderService;
import com.zzhua.cglib.service.OrderServiceImpl;
import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Method;

/**
 * @description
 * @Author: zzhua
 * @Date 2022/1/7
 */
public class Test03 {

    public static void main(String[] args) {

        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\projects\\dynamic-proxy-demo\\target\\classes");

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(OrderServiceImpl.class);

        Callback_0 callback_0 = new Callback_0();
        Callback_2 callback_2 = new Callback_2();

        enhancer.setCallbacks(new Callback[]{callback_0, callback_2});

        enhancer.setCallbackFilter(new CallbackFilter() {
            @Override
            public int accept(Method method) {
                if ("sayHello".equals(method.getName())) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });


        OrderService orderService = (OrderService) enhancer.create();

        orderService.sayHello();
        orderService.sayWorld();

    }

}
